package com.study.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.book.common.R;
import com.study.book.dto.LibrarySeatDetailDto;
import com.study.book.mapper.*;
import com.study.book.pojo.*;
import com.study.book.service.LibraryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author ASUS
* @description 针对表【Library】的数据库操作Service实现
* @createDate 2024-05-05 18:18:07
*/
@Service
public class LibraryServiceImpl extends ServiceImpl<LibraryMapper, Library>
    implements LibraryService{

    @Autowired
    private LibraryMapper libraryMapper;

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private LibrarySeatDetailMapper seatDetailMapper;

    @Override
    public List<Library> getRecommendLibraryList() {
        return libraryMapper.selectList(new LambdaQueryWrapper<Library>().orderByDesc(Library::getRecommend));
    }

    @Override
    public List<Library> getAreaLibraryList(String area) {
        return libraryMapper.selectList(new LambdaQueryWrapper<Library>().eq(Library::getArea, area).orderByDesc(Library::getRecommend));
    }


    @Override
    public R searchSeat(LibrarySeatDetailDto dto) {
        List<LibrarySeatDetail> librarySeatDetails = seatDetailMapper.selectList(Wrappers.<LibrarySeatDetail>lambdaQuery()
                .eq(LibrarySeatDetail::getStatus, dto.getStatus())
                .eq(LibrarySeatDetail::getLibraryid, dto.getLibraryid())
                .eq(LibrarySeatDetail::getSeatTime, dto.getSeatTime()));
        return R.ok(librarySeatDetails);
    }

    @Override
    public R creantSeatZZ(LibrarySeatDetailDto dto) {
        //判断是否已经占座
        Integer count = seatDetailMapper.selectCount(Wrappers.<LibrarySeatDetail>lambdaQuery()
                .eq(LibrarySeatDetail::getStatus, dto.getStatus())
                .eq(LibrarySeatDetail::getSeatId, dto.getSeatId())
                .eq(LibrarySeatDetail::getLibraryid, dto.getLibraryid())
                .eq(LibrarySeatDetail::getSeatTime, dto.getSeatTime()));
        if (count>0){
            return R.error("座位已经被占");
        }
        LibrarySeatDetail  librarySeatDetail= new LibrarySeatDetail();
        BeanUtils.copyProperties(dto,librarySeatDetail);
        librarySeatDetail.setSeatTime(dto.getSeatTime());
        librarySeatDetail.setCreatTime(new Date());
        librarySeatDetail.setFlag("1");
        seatDetailMapper.insert(librarySeatDetail);
        return R.ok("占座成功");
    }
}




